# ~*~ coding: utf-8 ~*~
import json

from flask import request, views, render_template, jsonify
from apps.perms.mixin import PermissionRequiredMixin, LoginRequiredMixin
from apps.extensions import db
from apps.common import response
from ..models import Node


__all__ = [
    'NodeTreeView', 'NodeTreeAPI',  'NodeAddChildrenAPI',
    'NodeDeleteAPI', 'NodeRenameAPI', 'NodeMoveChildrenAPI'
]


class NodeTreeView(PermissionRequiredMixin, LoginRequiredMixin, views.MethodView):
    permission_required = 'assets-asset-list'

    def get(self):
        return render_template('assets/_node_tree.html')


class NodeTreeAPI(PermissionRequiredMixin, LoginRequiredMixin, views.MethodView):
    permission_required = 'assets-node-tree'

    def has_perm(self):
        return True

    def get(self):
        key = request.args.get('key')
        type = int(request.args.get('type'))
        if key:
            node = db.session.query(Node).filter(Node.key == key).first()
            is_initial = False
        else:
            node = Node.root()
            is_initial = True

        if is_initial:
            with_self = True
        else:
            with_self = False

        if type == 1:
            nodes = node.get_children(with_self=with_self)
            results = [node.as_tree_node() for node in nodes]
        elif type == 2:
            results = node.as_tree_node2()
        else:
            results = []

        return jsonify(results)


class NodeAddChildrenAPI(PermissionRequiredMixin, LoginRequiredMixin, views.MethodView):
    permission_required = 'assets-node-add_children'

    def has_perm(self):
        return True

    def post(self, pk):
        instance = db.session.query(Node).get(pk)
        child = instance.create_child('新节点')
        return response.success(data=child.as_tree_node())


class NodeMoveChildrenAPI(PermissionRequiredMixin, LoginRequiredMixin, views.MethodView):
    permission_required = 'assets-node-move_children'

    def has_perm(self):
        return True

    def post(self, pk):
        instance = db.session.query(Node).get(pk)

        nodes_id = request.form.get('nodes')
        nodes_id = json.loads(nodes_id)
        children = db.session.query(Node).filter(Node.id.in_(nodes_id)).all()
        for node in children:
            if not node:
                continue
            node.parent = instance
        return response.success()


class NodeDeleteAPI(PermissionRequiredMixin, LoginRequiredMixin, views.MethodView):
    permission_required = 'assets-node-delete'

    def has_perm(self):
        return True

    def post(self, pk):
        instance = db.session.query(Node).get(pk)
        if not instance.children:
            instance.delete()
            return response.success()
        else:
            return response.params_error(data={
                'message': '存在子节点, 不能删除'
            })


class NodeRenameAPI(PermissionRequiredMixin, LoginRequiredMixin, views.MethodView):
    permission_required = 'assets-node-rename'

    def has_perm(self):
        return True

    def post(self, pk):
        instance = db.session.query(Node).get(pk)
        instance.value = request.form.get('value')
        instance.update()
        return response.success()
